package com.jerome.election.server;

import com.jerome.election.config.ConfigUtil;
import com.jerome.election.constants.AppConstant;
import com.jerome.election.http.server.HttpServer;
import com.jerome.election.server.handler.MemberHandler;

import java.io.IOException;
import java.util.Map;

/**
 * 每个成员服务
 * @author jerome
 * @date 2023/10/18
 */
public class MemberServer {

    public static void main(String[] args) {
        try {
            Map<String, String> paramsMap = ConfigUtil.getMap(args);
            String config_path = paramsMap.get(AppConstant.CONFIG_PATH);
            if (config_path == null) {
                System.out.println("Pass the parameter config_path, such as config_path=/tmp/config.properties");
                return;
            }
            //读取配置
            ContextUtil.initConf(config_path);
            //启动成员提供者服务
            HttpServer.startServer(
                    Integer.parseInt(ContextUtil.getConfig().getServerMap().get(ContextUtil.getConfig().getNode()).split(":")[1])
                    ,new MemberHandler());
            //初始化上下文
            ContextUtil.initContext();
            System.out.println(String.format("MemberServer %d start",ContextUtil.getConfig().getNode()));
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
